from ..utils.crypto_utils import server_utils
from ..utils.logger import logger


class BackendCourse:

    @staticmethod
    def orders_add(course_ids, user_phone, userinfo_id, order_no):
        """
        下单普通课程
        """
        logger.info("------推送普通课程------")
        try:
            data_dict = {"courseIds": course_ids,
                         "courseCount": "6",
                         "isGifts": "2",
                         "giftPadLowestPrice": "12",
                         "popUpPromptSwitch": "0",
                         "displayPlatform": "ALL",
                         "showUserName": user_phone,
                         "userInfoId": userinfo_id,
                         "userName": user_phone,
                         "orderType": "1",
                         "sellType": "2",
                         "mobilePhone": user_phone,
                         "orderNo": order_no,
                         "vTotalAmount": "0.01",
                         "costPrice": "0.01",
                         "payType": "2",
                         "validityType": "1",
                         "discount": "0.7",
                         "Gift": "2",
                         "_validator_ajaxsubmit": True
                         }
            return server_utils.post_request(url=f"/orders/add.html", data=data_dict).json()
        except Exception as e:
            server_utils.handle_exception(e, "下单普通课程失败")

    @staticmethod
    def paper_course_push(user_info_ids, paper_course_id, expire_time):
        """
        推送纸上课堂课程
        """
        try:
            data_dict = {"userInfoIds": user_info_ids,
                         "id": paper_course_id,
                         "expireTime": expire_time,
                         }
            return server_utils.post_request(url=f"/uc/v1/paperCourse/simple/pushPaper", data=data_dict).json()
        except Exception as e:
            server_utils.handle_exception(e, "推送纸上课堂课程失败")


backend_course = BackendCourse()
